package com.turing.post.account.impl;

import java.math.BigInteger;
import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.account.util.MasterAccount;
import com.turing.post.account.util.MasterAccountBusiness;
import com.turing.post.account.util.MasterAccountDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "account.impl.MasterAccountBusinessImpl")
@Remote
@Local
public class MasterAccountBusinessImpl implements MasterAccountBusiness {

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@EJB(mappedName = "account.impl.MasterAccountDaoImpl")
	private MasterAccountDao masterAccountDaoImpl;

	/**
	 * 创建主帐户
	 * 
	 * @param issueCompanyNo
	 * @return
	 */
	public BigInteger createMasterAccount(String issueCompanyNo, Date openTime,
			String cardNo, BigInteger ownerId, String remark)
			throws PCPSApplicationException {
		// 产生主帐号
		BigInteger masterAccNo = pcpsCommonDaoImpl.getUniqueIntegerKey();

		// 设置记表参数
		MasterAccount masteracc = new MasterAccount();

		masteracc.setMasterAccNo(masterAccNo);
		masteracc.setIssueCompanyNo(issueCompanyNo);
		masteracc.setCardNo(cardNo);
		masteracc.setOwnerId(ownerId);
		masteracc.setBalance(BigInteger.ZERO);
		masteracc.setOpenTime(openTime);
		masteracc.setLastAccountTime(openTime);
		masteracc.setOpenStatus(MasterAccount.MASTERACCOUNT_OPENSTATUS_NORMAL);
		masteracc.setCloseStatus(MasterAccount.MASTERACCOUNT_CLOSESTATUS_NONE);
		masteracc.setFreeze(MasterAccount.MASTERACCOUNT_FREEZE_NONE);
		masteracc.setFreezeAmount(BigInteger.ZERO);
		masteracc.setAuthDeposit(BigInteger.ZERO);
		masteracc.setAuthRedemp(BigInteger.ZERO);
		masteracc.setStopPayment(MasterAccount.MASTERACCOUNT_STOPPAYMENT_NONE);
		masteracc.setRemark(remark);

		// 记表
		masterAccountDaoImpl.insert(masteracc);

		return masterAccNo;
	}

	/**
	 * 更新主帐户余额
	 * 
	 * @param masterAccNo
	 *            主帐号
	 * @param amount
	 *            发生额
	 * @param accTime
	 *            记帐时间
	 */
	public BigInteger updateBalance(BigInteger masterAccNo, BigInteger amount,
			Date accTime) throws PCPSApplicationException {

		// 以锁定方式根据主帐号检索主帐户信息
		MasterAccount mac = masterAccountDaoImpl
				.getMasterAccountByMasterAccNoForUpdate(masterAccNo);
		if (mac == null)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
					TuxedoRespCode.RC_INVALID, "主帐号" + masterAccNo.toString()
							+ "不存在!");

		// 设置记帐时间
		mac.setLastAccountTime(accTime);

		// 更新余额
		mac.addAmount(amount);

		// 检查余额
		if (mac.getBalance().compareTo(BigInteger.ZERO) < 0)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
					TuxedoRespCode.RC_LACK, "主帐号" + masterAccNo.toString()
							+ "记帐金额" + amount.toString() + "/"
							+ mac.getBalance().toString() + "非法!");

		BigInteger alllim = mac.getFreezeAmount() /* .add(mac.getAuthDeposit()) */
				.add(mac.getAuthRedemp());

		// 检查限制金额
		if (alllim.compareTo(BigInteger.ZERO) > 0
				&& mac.getBalance().compareTo(alllim) < 0)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
					TuxedoRespCode.RC_LACK, "主帐号" + masterAccNo.toString()
							+ "记帐" + amount.toString() + "/"
							+ mac.getBalance().toString() + "后余额不足限制金额!");

		// 更新
		masterAccountDaoImpl.update(mac);

		// 返回更新后的余额
		return mac.getBalance();
	}
}
